<?php

namespace App\Http\Controllers\Manage;

use App\Http\Controllers\ApiController;
use App\Http\Requests\HpiSubStoreRequest;
use App\Http\Requests\HpiSubUpdateRequest;
use App\Models\MedicalHpiSub;
use App\Project\Filters\HpiSubFilter;
use App\Project\Transform;
use App\Transformers\HpiSubTransformer;
use Carbon\Carbon;
use Illuminate\Http\Request;

class HpiSubController extends ApiController
{
    /**
     * 现病史组项目存储
     * @param \App\Http\Requests\HpiSubStoreRequest $request
     * @param \App\Models\MedicalHpiSub             $hpiSub
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(HpiSubStoreRequest $request, MedicalHpiSub $hpiSub)
    {
        $record_id = $request->input('record_id');

        $count = $hpiSub->where(['record_id' => $record_id])->count();

        if ($count == 5) {
            return $this->setMessage('现病史组数量已达到最多！')->setCode('400004')->response();
        }

        $input = (new HpiSubFilter($request))->except(['id']);

        if (isset($input['started_at']) && !empty($input['started_at'])) {
            $input['started_at'] = Carbon::parse($input['started_at'])->startOfMonth()->toDateString();
        } else {
            $input['started_at'] = Carbon::now()->startOfMonth()->toDateString();
        }

        if (isset($input['symptoms']) && !empty($input['symptoms'])) {
            $input['symptoms'] = implode(',', $input['symptoms']);
        }

        if ($id = $request->input('id')) {
            $model = $hpiSub->where('id', $id)->update($input);
        } else {
            $model = $hpiSub->create($input);
        }
        return $this->response(['id' => $model['id']]);
    }

    /**
     * 现病史组信息
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        $data = null;
        $id   = $request->get('id');

        if ($id > 0) {

            $data = MedicalHpiSub::findOrFail($id);
        } else {
            abort(400, '缺失现病史组ID');
        }

        return $this->item($data, new HpiSubTransformer());
    }

    /**
     * @param \App\Http\Requests\HpiSubUpdateRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(HpiSubUpdateRequest $request)
    {
        $input = (new HpiSubFilter($request))->except(['id', 'record_id']);

        if (isset($input['started_at']) && !empty($input['started_at'])) {
            $input['started_at'] = Carbon::parse($input['started_at'])->startOfMonth()->toDateString();
        } else {
            $input['started_at'] = Carbon::now()->startOfMonth()->toDateString();
        }

        $input = (new Transform())->setArray($input)
                                  ->convertArrayToString(['symptoms', 'diagnose'])
                                  ->convertArrayToJson([
                                        'medication',
                                        'cn_medication',
                                        'before_treatment',
                                        'after_treatment'
                                    ])->get();

        $result = MedicalHpiSub::where(['id' => $request->input('id')])->update($input);

        return $this->responseForUpdate($result);
    }

    public function destroy(MedicalHpiSub $hpiSub)
    {
        return $this->responseForDelete($hpiSub->delete());
    }

    public function multiple(Request $request)
    {

    }
}
